import { Injectable } from '@nestjs/common';
import { FindManyOptions, In, Repository } from 'typeorm';
import { PermEntity } from './entities/perm.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { PermWidthMenuEntity } from './entities/perm-width-menu.entity';
import { UserDto } from '../user/user.decorator';

@Injectable()
export class PermService {
  constructor(
    @InjectRepository(PermEntity)
    private readonly permEntityRep: Repository<PermEntity>,
    @InjectRepository(PermWidthMenuEntity)
    private readonly permWidthMenuEntityRep: Repository<PermWidthMenuEntity>,
  ) {}

  findPerms(where: FindManyOptions<PermEntity>) {
    return this.permEntityRep.find(where);
  }

  // 根据权限 获取用户页面资源
  async getUserPageIds(user: UserDto) {
    const userPermIds = user.perms.map((item) => item.permId);
    const userPage = await this.permWidthMenuEntityRep.find({
      where: {
        permId: In(userPermIds),
      },
    });
    return userPage.map((item) => item.pageId);
  }
}
